import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTasklistStore = defineStore('tasklist', () => {
  var tasklist = ref([])
  var countNum = computed(()=>{
    return tasklist.value.filter((item)=>item.status==false).length
  })
  var addTask = (payload) => {
    if (payload) {
      tasklist.value.push({ id: tasklist.value.length + 1, name: payload, status: false })
      console.log(tasklist.value);
    }
  }
  var change = (payload) => {
    var task = tasklist.value.find((item)=> {
      return item.id = payload
    })
    if (task) {
      task.status = !task.status
      console.log(task);
      console.log(task.status);
    }
  }
  var deleteTask = (payload) => {
    var index = tasklist.value.findIndex((item) => {
      return item.id = payload
    })
    if (index != -1) {
      tasklist.value.splice(index,1)
    }
  }


  return {
    tasklist,
    addTask,
    change,
    countNum,
    deleteTask
  }

})
